package com.wang.controller;


import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.common.Constants;
import com.wang.common.Result;
import com.wang.entity.Dict;
import com.wang.entity.Menu;
import com.wang.mapper.DictMapper;
import com.wang.mapper.MenuMapper;
import com.wang.service.MenuService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wy
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;


    @Resource
    private DictMapper dictMapper;
    @Resource
    private MenuMapper menuMapper;

    private StringRedisTemplate stringRedisTemplate;
    // 新增或者更新
    @PostMapping("/add")
    public Result save(@RequestBody Menu menu) {
        menuService.saveOrUpdate(menu);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        menuService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        menuService.removeByIds(ids);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(menuService.getById(id));
    }


//    @GetMapping
//    public Result findAll(@RequestParam(defaultValue = "") String name){
//        //1.从缓存获取数据
//        String jsonStr = stringRedisTemplate.opsForValue().get(Constants.FILES_KEY);
//        List<Menu> menus;
//        if(StrUtil.isBlank(jsonStr)){
//            menus=menuService.findMenus(name);
//            stringRedisTemplate.opsForValue().set(Constants.FILES_KEY, JSONUtil.toJsonStr(menus));
//        }else{
//            menus=JSONUtil.toBean(jsonStr, new TypeReference<List<Menu>>() {
//            },true);
//        }
//        return Result.success(menuService.findMenus(name));
//    }
    @GetMapping
    public Result findAll(){
        //1.从缓存获取数据
        String jsonStr = stringRedisTemplate.opsForValue().get(Constants.FILES_KEY);
        List<Menu> menus;
        if(StrUtil.isBlank(jsonStr)){
            menus = menuMapper.selectList(null);
            stringRedisTemplate.opsForValue().set(Constants.FILES_KEY, JSONUtil.toJsonStr(menus));
        }else{
            menus=JSONUtil.toBean(jsonStr, new TypeReference<List<Menu>>() {
            },true);
        }
        return Result.success(menus);
    }

    @GetMapping("/icons")
    public Result getIcons() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return Result.success(dictMapper.selectList(queryWrapper));
    }
}

